<?php
declare(strict_types=1);

namespace longbingcore\wxcore;

use app\Common\LongbingServiceNotice;

use app\farm\model\Config;
use think\facade\Db;

/**
 * 达达开放平台sdk
 * User: 仝帅
 * Date: 2016-12-19
 * Time: 16:48
 */
class YsCloudApi
{

    protected $appId;

    protected $appKey;

    protected $signature;

    protected $url;

    protected $uniacid;



    public function __construct($uniacid)
    {
        $this->uniacid= $uniacid;

        $config_model = new Config();

        $config = $config_model->dataInfo(['uniacid'=>$uniacid]);

        $this->AppKey = $config['ys_appkey'];

        $this->Secret = $config['ys_secret'];

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-17 17:42
     * @功能说明:获取令牌
     */

    public function getToken(){

        $url = 'https://open.ys7.com/api/lapp/token/get';

        $key = 'yscloud_token';

        $token = getCache($key,$this->uniacid);

        $post_data = [

            'appKey' => $this->AppKey,

            'appSecret' => $this->Secret
        ];

        if(empty($token)){

            $data = lbCurlPost($url,$post_data);

            $data = json_decode($data,true);

            if(!empty($data['code'])&&$data['code']==200){

                $token = $data['data']['accessToken'];

                setCache($key,$token,86400*6,$this->uniacid);

            }else{

                $token = '';
            }

        }

        return $token;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-17 17:54
     * @功能说明:获取空间列表
     */
    public function getVideoList(){

        $url = 'https://open.ys7.com/api/lapp/live/video/list';

        $post_data = [

            'accessToken' => $this->getToken(),

            'pageSize'    => 100
        ];

        $data = lbCurlPost($url,$post_data);

        $data = json_decode($data,true);

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-17 17:57
     * @功能说明:获取空间详情
     */
    public function getVideoInfo($deviceSerial,$channelNo=1){

        $url = 'https://open.ys7.com/api/lapp/v2/live/address/get';

        $key = $deviceSerial.'-2'.$channelNo;

        $url_data = getCache($key,$this->uniacid);

        if(empty($url_data)){

            $post_data = [

                'accessToken' => $this->getToken(),

                'deviceSerial'=> $deviceSerial,

                'channelNo'   => $channelNo,

                'protocol'    => 2,

                'quality'     => 2
            ];

            $data = lbCurlPost($url,$post_data);

            $data = @json_decode($data,true);

            if(isset($data['code'])&&$data['code']==200){

                $url_data = $data['data']['url'];

                setCache($key,$url_data,86400,$this->uniacid);
            }

        }

        return $url_data;

    }


    /**
     * @param $deviceSerial
     * @param $direction
     * @param int $channelNo
     * @功能说明:开始转动
     * @author chenniang
     * @DataTime: 2022-04-12 10:51
     */
    public function startTurn($deviceSerial,$direction,$channelNo=1)
    {
        $url='https://open.ys7.com/api/lapp/device/ptz/start';

        $post_data = [

            'accessToken' => $this->getToken(),

            'deviceSerial'=> $deviceSerial,

            'channelNo'   => $channelNo,

            'direction'   => $direction,

            'speed'       => 1
        ];

        $data = lbCurlPost($url,$post_data);

        $data = json_decode($data,true);

        return $data;
    }




    /**
     * @param $deviceSerial
     * @param $direction
     * @param int $channelNo
     * @功能说明:停止转动
     * @author chenniang
     * @DataTime: 2022-04-12 10:51
     */
    public function stopTurn($deviceSerial,$direction=0,$channelNo=1)
    {
        $url='https://open.ys7.com/api/lapp/device/ptz/stop';

        $post_data = [

            'accessToken' => $this->getToken(),

            'deviceSerial'=> $deviceSerial,

            'channelNo'   => $channelNo,

            'direction'   => $direction,

        ];

        $data = lbCurlPost($url,$post_data);

        $data = json_decode($data,true);

        return $data;
    }




    /**
     * @param $url
     * @param $post_data
     * @param string $post_type
     * @功能说明:发送请求
     * @author chenniang
     * @DataTime: 2021-10-26 14:38
     */
    public function request($url,$post_data,$post_type='POST'){

        $curl = curl_init();

        $post_data = json_encode($post_data);

        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => $post_type,
            CURLOPT_POSTFIELDS => $post_data,
            CURLOPT_HTTPHEADER => array(
                "Content-Type:application/json",
                "User-Agent:openApi",
                "Content-Type:application/json; charset=utf-8",
                "accept-encoding:gzip,deflate",
                "time-stamp:".time(),
//                "data-signature:D2C3D3C7289EAE111277999D21196D4D",
                "data-signature:".strtoupper(md5($this->appKey.$post_data))
            ),
        ));

        $response = curl_exec($curl);

        $err = curl_error($curl);

        curl_close($curl);

        return json_decode($response,true);

//        if ($err) {
//            echo "cURL Error #:" . $err;
//        } else {
//            echo $response;
//        }
    }





}